package com.mrsphoto.mrphoto.presenter;

import android.os.Handler;

import com.mrsphoto.mrphoto.activity.HomeActivity;
import com.mrsphoto.mrphoto.bean.User;
import com.mrsphoto.mrphoto.impl.Userimpl;
import com.mrsphoto.mrphoto.model.IuserLoginControl;
import com.mrsphoto.mrphoto.model.IuserLoginView;
import com.mrsphoto.mrphoto.model.OnLoginListener;

/**
 * Created by King on 2016/6/24.
 */
public class UserLoginPresenter {
    private IuserLoginControl userlogin;
    private IuserLoginView userView;
    private Handler mhandler=new Handler();

    public UserLoginPresenter(IuserLoginView userView) {
        this.userView = userView;
        userlogin=new Userimpl();
    }
    public void login(){
        userView.showLoading();
        userlogin.login(new User(userView.getUserName(),userView.getPassword()),new OnLoginListener() {
            @Override
            public void loginSuccess(final User user) {

                //需要在UI线程执行
                mhandler.post(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        userView.toMainActivity(user);
                        userView.hideLoading();
                        userView.intentView(HomeActivity.class);
                    }
                });


            }

            @Override
            public void loginFailed() {

                //需要在UI线程执行
                mhandler.post(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        userView.hideLoading();
                        userView.showFailedError();
                    }
                });

            }
        });
    }
    public void clear()
    {
        userView.clearUserName();
        userView.clearPassword();
    }

}
